package org.example.bookingsystem.Listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.bookingsystem.client.SystemClient;
import org.example.bookingsystem.common.Constants;
import org.example.bookingsystem.model.Result;
import org.example.bookingsystem.model.Ticket;
import org.example.bookingsystem.service.SystemService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Listener1 {
    @Autowired
    SystemClient systemClient;
    @Autowired
    private SystemService systemService;
    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = Constants.REQUEST_QUEUE)
    @SendTo(Constants.RESPONSE_QUEUE) // 自动将响应发送到响应队列
    public String queryResponse(String message) throws JsonProcessingException {
        if ("query".equalsIgnoreCase(message)) {
            return objectMapper.writeValueAsString(systemService.selectAllTickets());
        }else if("true".equalsIgnoreCase(message)){
            return systemClient.queryBank();
        }else if("false".equalsIgnoreCase(message)){
            return "服务结束";
        }else if("advice".equalsIgnoreCase(message)){
            int tid = 3;
            return String.valueOf(tid);
        }else if(message.contains(",")){
            return systemClient.pay(message);
        }

        return null;
    }
//    public String acceptResponse(String message) {
//        if("true".equalsIgnoreCase(message)) {
//
//        }else {
//            return "服务结束";
//        }
//        return null;
//    }
}
